﻿/*
 * VUIManualInputWindow#FILEEXTENSION#
 * #PROJECTNAME#
 * Created by #SMARTDEVELOPERS# on #CREATEONDATE#.
 */

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;

using UnityEngine.UI;
using VFramework;
namespace VFramework
{
	/// <summary>
	/// 手动输入数字的窗口，0-9的按钮，确认，清理的按钮
	/// </summary>
	public class VUIManualInputWindow : VUIPopWindow
	{

		public List<Button> numberBtns;

		public Button backBtn;
		public Button clearBtn;

		public delegate void SureCB (int v);
		public Text vl;
		private string value = "0";
		private int max_value;

		private SureCB _cb;

		void updateUI ()
		{
			try {
				int v = int.Parse (value);
				if (v > max_value)
					v = max_value;
				value = v + "";
			} catch (Exception e) {
				VLogger.debug (e.ToString ());
				value = "0";
			}
			vl.text = value;
		}

		protected override void _start ()
		{
			if (numberBtns != null && numberBtns.Count > 0) {
				for (int i = 0; i < numberBtns.Count; i++) {
					Button b = numberBtns [i];
					string index = i + "";
					b.onClick.AddListener (delegate () {
						value += index;
						updateUI ();
						EventMgr.getInstance ().dispatchEvent ("normal_click_effect");
					});
				}

			}

			backBtn.onClick.AddListener (delegate () {
				value = value.Substring (0, value.Length - 1);
				updateUI ();
				EventMgr.getInstance ().dispatchEvent ("normal_click_effect");
			});

			clearBtn.onClick.AddListener (delegate () {
				value = "0";
				updateUI ();
				EventMgr.getInstance ().dispatchEvent ("normal_click_effect");
			});

			updateUI ();

			base._start ();
		}

		public void show (SureCB cb, int value, int max_value = 99)
		{
			_cb = cb;
			this.max_value = max_value;
			this.value = value + "";
			updateUI ();
			base.show ();
		}

		public override void sure ()
		{
			if (_cb != null) {
				_cb.Invoke (Int32.Parse (value));
			}
			hide ();
		}

		void Start ()
		{

		}

		void Update ()
		{

		}
	}
}